package com.itheima.controller;

import com.itheima.pojo.Brand;
import com.itheima.pojo.Page;
import com.itheima.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘全
 * @date 2021/9/16
 */
@RestController
@RequestMapping("/brands")
public class BrandController {

    @Autowired
    private BrandService bs;

    @GetMapping
    public Result selectAll() {
        List<Brand> brands = bs.selectAll();
        boolean flag = brands != null;
        String msg = flag ? "" : "查询失败,请重试!";
        return new Result(flag, msg, brands);
    }

    @PostMapping
    public Result add(@RequestBody Brand brand) {
        int add = bs.add(brand);
        boolean flag = add > 0;
        String msg = flag ? "添加成功" : "添加失败";
        return new Result(flag, msg);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id) {
        int i = bs.deleteById(id);
        String msg = i > 0 ? "删除成功" : "删除失败";
        return new Result(i>0,msg);
    }

    @PutMapping
    public Result update(@RequestBody Brand brand) {
        int i = bs.update(brand);
        String msg = i > 0 ? "修改成功" : "修改失败";
        return new Result(i>0,msg);
    }

    @RequestMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody int[] ids) {
        int i = bs.deleteByIds(ids);
        String msg = i > 0 ? "删除成功" : "删除失败";
        return new Result(i>0,msg);
    }

    @RequestMapping("/selectPage")
    public Result selectPage(@RequestParam("index") int index, @RequestParam("pageSize") int pageSize) {
        Page<Brand> brandPage = bs.selectPage(index, pageSize);
        boolean flag = brandPage != null;
        String msg = flag ? "" : "查询失败,请重试!";
        return new Result(flag,msg,brandPage);
    }

    @RequestMapping("/selectByPageAndCondition")
    public Result selectByPageAndCondition(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestBody Brand brand) {
        Page<Brand> brandPage = bs.selectByPageAndCondition(page, pageSize, brand);
        boolean flag = brandPage != null;
        String msg = flag ? "" : "查询失败,请重试!";
        return new Result(flag,msg,brandPage);
    }
}
